package com.presto.directo.mvvm.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.presto.directo.entity.QuestionEntity
import com.presto.directo.net.getHttpMap
import com.presto.directo.net.sign
import com.presto.directo.utils.*

class QuestionViewModel(application: Application) : BaseViewModel(application) {

    val uploadLiveData = MutableLiveData<Any>()
    val infoLiveData = MutableLiveData<QuestionEntity>()

    var question_1 = -1
    var question_2 = -1
    var question_3 = -1
    var question_4 = -1
    var question_5 = -1
    var question_6 = -1
    var question_7 = -1

    fun getInfo() {
        start(onNext = {
            showProgress()
            getApiService().getQuestion(getHttpMap().sign(18)).getResultOrNull()?.let {
                question_1 = it.question_1.toIntOrNull() ?: -1
                question_2 = it.question_2.toIntOrNull() ?: -1
                question_3 = it.question_3.toIntOrNull() ?: -1
                question_4 = it.question_4.toIntOrNull() ?: -1
                question_5 = it.question_5.toIntOrNull() ?: -1
                question_6 = it.question_6.toIntOrNull() ?: -1
                question_7 = it.question_7.toIntOrNull() ?: -1
                infoLiveData.value = it
            }
        }, onComplete = {
            dismissProgress()
        })
    }

    fun upload() {
        val map = getHttpMap()
        map["question_1"] = question_1
        map["question_2"] = question_2
        map["question_3"] = question_3
        map["question_4"] = question_4
        map["question_5"] = question_5
        map["question_6"] = question_6
        map["question_7"] = question_7
        start(onNext = {
            showProgress()
            getApiService().saveQuestion(map.sign(17)).getResultOrNull()
            uploadLiveData.value = true
        }, onComplete = {
            dismissProgress()
        })
    }

}